package com.src.cy.loadview.dialog;

import android.app.Dialog;
import android.content.Context;

import com.src.cy.loadview.R;
import com.src.cy.loadview.view.LoadView;

/**
 * Created by CY on 2016/4/11.
 */
public class LoadDialog extends Dialog {

    private static LoadDialog mLoadDialog;
    private LoadView mLoadView;

    public LoadDialog(Context context) {
        super(context);
        init();
    }

    public LoadDialog(Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    protected LoadDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    private void init() {
        getWindow().setDimAmount(0f);
        getWindow().setWindowAnimations(R.style.LoadDialog);
        mLoadView = (LoadView) getLayoutInflater().inflate(R.layout.layout_load_dialog, null);
        setContentView(mLoadView);
    }

    public static LoadDialog showLoadDialog(Context context) {

        showLoadDialog(context, context.getResources().getString(R.string.loading));
        return mLoadDialog;
    }

    public static LoadDialog showLoadDialog(Context context, String message) {
        if (mLoadDialog == null) {
            mLoadDialog = new LoadDialog(context);
        }
        mLoadDialog.mLoadView.setText(message);
        if (!mLoadDialog.isShowing()) {
            try {
                mLoadDialog.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mLoadDialog;
    }

    public static void dismissLoadDialog() {
        if (mLoadDialog != null && mLoadDialog.isShowing()) {
            mLoadDialog.dismiss();
            mLoadDialog = null;
        }
    }
}
